package com.tanhua.dubbo.api;

import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi{
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Integer save(Comment comment) {
        //1、查询动态
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        //2、向comment对象设置被评论人属性
        if(movement!=null){
            comment.setPublishUserId(movement.getUserId());
        }
        //保存到数据库
        mongoTemplate.save(comment);
        //更新动态表中的对应字段
        Query query=Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update=new Update();
        //分辨点赞,评论,喜欢,对应+1
        if(comment.getCommentType()== CommentType.LIKE.getType()){
            update.inc("likeCount",1);
        }else if(comment.getCommentType()== CommentType.COMMENT.getType()){
            update.inc("commentCount",1);
        }else {
            update.inc("loveCount",1);
        }
        //设置更新参数
        FindAndModifyOptions options=new FindAndModifyOptions();
        //获取更新后的最新数据
        options.returnNew(true);
        Movement modify = mongoTemplate.findAndModify(query, update,options, Movement.class);
        //5、获取最新的评论数量，并返回
        Integer count = modify.statisCount(comment.getCommentType());
        return count;
    }

    @Override
    public List<Comment> commentList(String movementId,CommentType commentType,int page,int pagesize) {
        //根据movementId获取动态详情
        Query query=Query.query(Criteria.where("publishId").is(new ObjectId(movementId))
                .and("commentType").is(commentType.getType())).with(Sort.by(Sort.Order.desc("created")))
                .skip((page-1)*pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return comments;
    }

    @Override
    public Boolean like(String id, Long userId, CommentType like) {
        //构造查询参数
        Criteria c=Criteria.where("publishId").is(new ObjectId(id))
                .and("userId").is(userId)
                .and("commentType").is(like.getType());
        Query query=Query.query(c);
        //判断是否存在,并返回
        return mongoTemplate.exists(query,Comment.class);
    }

    @Override
    public Integer delete(Comment comment) {
        //构造查询参数
        Criteria c=Criteria.where("publishId").is(comment.getPublishId())
                .and("userId").is(comment.getUserId())
                .and("commentType").is(comment.getCommentType());
        Query query=Query.query(c);
        mongoTemplate.remove(query,Comment.class);
        //2、修改动态表中的总数量
        Query movementQuery = Query.query(Criteria.where("id").is(comment.getPublishId()));
        //分辨点赞,评论,喜欢,对应-1
        Update update=new Update();
        if(comment.getCommentType()== CommentType.LIKE.getType()){
            update.inc("likeCount",-1);
        }else if(comment.getCommentType()== CommentType.COMMENT.getType()){
            update.inc("commentCount",-1);
        }else {
            update.inc("loveCount",-1);
        }
        //设置更新参数
        FindAndModifyOptions options=new FindAndModifyOptions();
        //获取更新后的最新数据
        options.returnNew(true);
        Movement modify = mongoTemplate.findAndModify(movementQuery, update,options, Movement.class);
        //5、获取最新的数量，并返回
        Integer count = modify.statisCount(comment.getCommentType());
        return count;
    }

    @Override
    public List<Comment> likes(Long userId, Integer page, Integer pagesize, CommentType comment) {
        Query query = Query.query(Criteria.where("publishUserId").is(userId)
                .and("commentType").is(comment.getType()))
                .skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return comments;
    }

    //判断comment数据是否存在
    @Override
    public Boolean hasContent(String commentId, Long userId) {
        Query query = Query.query(Criteria.where("publishId")
                .is(new ObjectId(commentId)).and("userId").is(userId));
        return mongoTemplate.exists(query, Comment.class);
    }


    @Override
    public Integer saveCL(Comment comment) {
        Comment oldComment = mongoTemplate.findById(comment.getPublishId(), Comment.class);
        if (oldComment != null) {
            comment.setPublishUserId(oldComment.getUserId());
        }
        mongoTemplate.save(comment);
        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        update.inc("likeCount", 1);
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Comment modify = mongoTemplate.findAndModify(query, update, options, Comment.class);
        return modify.getLikeCount();
    }


    @Override
    public Integer deleteCL(Comment comment) {
        Query query = Query.query(Criteria.where("publishId").is(comment.getPublishId())
                .and("userId").is(comment.getUserId()));
        mongoTemplate.remove(query, Comment.class);
        Query newQuery = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        update.inc("likeCount", -1);
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Comment modify = mongoTemplate.findAndModify(newQuery, update, options, Comment.class);
        return modify.getLikeCount();
    }
}
